from fib_config_v4 import y_scale, y_column, tezheng_v1
from matplotlib import pyplot as plt
import numpy as np


def buy_flow(df, dec, dec_feature):
    i = 0
    money = 50000
    err_count = 0
    count_map = {}
    err_count_map = {}
    sum_all = 0
    all_count = 0

    y_test = df[y_column].values
    total_n = len(y_test)
    x1 = list(range(total_n))
    x2 = list(map(lambda x: x * 0.1, x1))
    plt.figure(figsize=(20, 10))
    st = 1000
    et = 2000
    y_predict = y_scale * dec.predict(df[dec_feature].values)
    print(type(y_predict))
    print("实际值均值方差", np.mean(y_test), np.var(y_test))
    print("实际值最大小值", np.max(y_test), np.min(y_test))
    print("预测值均值方差", np.mean(y_predict), np.var(y_predict))
    print("预测值最大小值", np.max(y_predict), np.min(y_predict))
    from collections import Counter
    print('Counter(y_predict)\n', Counter(y_predict))
    # plt.scatter(dec.predict(df[dec_feature].values), y_test, s=np.pi, c=y_test, alpha=0.3)
    plt.plot(x2[st:et], y_predict[st:et], x2[st:et], y_test[st:et])
    plt.show()
    for res in dec.predict(df[dec_feature].values):
        if str(res) in count_map:
            count_map[str(res)] = count_map[str(res)] + 1
        else:
            count_map[str(res)] = 1
        if res > 0:
            all_count = all_count + 1
            print(res, "|||", y_test[i])
            if y_test[i] < 0:
                err_count = err_count + 1
                if str(res) in err_count_map:
                    err_count_map[str(res)].append(y_test[i])
                else:
                    err_count_map[str(res)] = [y_test[i]]
            # money = money * (1 + y_test[i] * y_scale/100)
            # money = money + 10000 * (y_test[i] * y_scale/100)
            sum_all = sum_all + y_test[i] * y_scale
            money = money * (1 + y_test[i] * y_scale / 100)
        i = i + 1
    print(count_map)
    print("error", err_count, err_count_map)
    print("--交易了----", all_count, ":", money, "mean", sum_all / all_count)


def buy_flow_v2(df, dec, dec_feature):
    i = 0
    money = 50000
    err_count = 0
    count_map = {}
    err_count_map = {}
    sum_all = 0
    all_count = 0
    y_test = df[y_column].values
    total_n = len(y_test)
    x1 = list(range(total_n))
    x2 = list(map(lambda x: x * 0.1, x1))
    plt.figure(figsize=(20, 10))
    st = 2000
    et = 2500
    # plt.scatter(dec.predict(df[dec_feature].values), y_test, s=np.pi, c=y_test, alpha=0.3)
    plt.plot(x2[st:et], dec.predict(df[dec_feature].values)[st:et], x2[st:et], y_test[st:et])
    plt.show()
    for res in dec.predict(df[dec_feature].values):
        round_res = round(res, 2)
        if str(round_res) in count_map:
            count_map[str(round_res)] = count_map[str(round_res)] + 1
        else:
            count_map[str(round_res)] = 1
        # 0.02 0.0218
        # 0.03 0.0238
        # 0.04 0.0239
        # 0.05 0.0230
        # 0.06 0.0212
        if res > 0.04:
            all_count = all_count + 1
            print(res, "|||", y_test[i], df.iloc[i][y_column], df.iloc[i]["date"], df.iloc[i]["symbol"])
            if y_test[i] < 0:
                err_count = err_count + 1
                if str(res) in err_count_map:
                    err_count_map[str(res)].append(y_test[i])
                else:
                    err_count_map[str(res)] = [y_test[i]]
            sum_all = sum_all + y_test[i]
            money = money * (1 + y_test[i])
        i = i + 1
    print(count_map)
    print("error", err_count, err_count_map)
    print("--交易了----", all_count, ":", money, "mean", sum_all / all_count)

